package com.guiwu.gteway.industryapp.Regist;

import android.support.annotation.NonNull;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.guiwu.gteway.industryapp.Api.GuiwuApi;
import com.guiwu.gteway.industryapp.Api.GuiwuApiCallback;
import com.guiwu.gteway.industryapp.util.ApiJsonUtil;
import com.guiwu.gteway.industryapp.util.JsonResult;

import org.json.JSONException;
import org.json.JSONObject;

import static com.google.common.base.Preconditions.checkNotNull;

public class RegisterPresenter implements RegisterContract.Presenter {

    private final RegisterContract.View mRegisterView;


    public RegisterPresenter(@NonNull RegisterContract.View registerView){
        this.mRegisterView = checkNotNull(registerView,"registerView can not be null");
      //  mRegisterView.setPresenter(this);  可以不需要
    }

    @Override
    public void start() {

    }

    @Override
    public void askForRegister(@NonNull String userName,@NonNull String company,@NonNull String mobile,
                               @NonNull String password, String email) {
        try{
            mRegisterView.showLoadingIndicator(true);
            String requestBody = assembleRegistJson(userName, company, mobile, password, email);
            GuiwuApi.userRegister(requestBody, new GuiwuApiCallback() {
                @Override
                public void onSuccess(String response) {
                    JsonObject jb = new JsonParser().parse(response).getAsJsonObject();
                    if (jb.get("errno").getAsInt() == 0){
                        mRegisterView.OnRegister(true,"success");
                    }else {
                        mRegisterView.OnRegister(false,jb.get("error").getAsString());
                    }
                    mRegisterView.showLoadingIndicator(false);
                }

                @Override
                public void onFailed(Exception e) {
                    mRegisterView.OnRegister(false,e.getMessage());
                    mRegisterView.showLoadingIndicator(false);
                }
            });
        }catch (JSONException ex){
            mRegisterView.OnRegister(false,ex.getMessage());
            mRegisterView.showLoadingIndicator(false);
        }


    }

    @Override
    public void getVerificationCode(@NonNull String poneNumber) {
        checkNotNull(poneNumber,"电话号码不能为空");
        try {
            String requestBody = assembleCodeJson(poneNumber);
            GuiwuApi.getVertifyCode(requestBody, new GuiwuApiCallback() {
                @Override
                public void onSuccess(String response) {
                    JsonResult result = ApiJsonUtil.getData(response);
                    if (result.getJsonElement() != null){
                        JsonObject object = result.getJsonElement().getAsJsonArray().get(0).getAsJsonObject();
                        String code = object.get("code").getAsString();
                        mRegisterView.onGetVerifyCode(code);
                    }else {
                        mRegisterView.showGetVerifyCodeError(result.getErrorMsg());
                    }
                }

                @Override
                public void onFailed(Exception e) {
                        mRegisterView.showGetVerifyCodeError(e.getMessage());
                }
            });

        } catch (JSONException e) {
            e.printStackTrace();
            mRegisterView.showGetVerifyCodeError(e.getMessage());
        }
    }


    //组装短信验证码请求
    private String assembleCodeJson(String poneNumber)throws JSONException{
        JSONObject requestContent = new JSONObject();
        requestContent.putOpt("mobile",poneNumber);
        return requestContent.toString();
    }

    private String assembleRegistJson(@NonNull String userName,@NonNull String company,@NonNull String mobile,
                                      @NonNull String password, String email)throws JSONException{
        JSONObject requestContent = new JSONObject();
        requestContent.putOpt("userName",userName);
        requestContent.putOpt("company",company);
        requestContent.putOpt("mobile",mobile);
        requestContent.putOpt("password",password);
        if (!email.equals("")){
            requestContent.putOpt("email",email);
        }
        return requestContent.toString();
    }

}
